//
//  JackBaseViewController.m
//  JackRadar
//
//  Created by Pei on 15/12/17.
//  Copyright © 2015年 Xiaobo Pei. All rights reserved.
//

#import "JackBaseViewController.h"

@interface JackBaseViewController ()<UINavigationControllerDelegate, UIGestureRecognizerDelegate>

@end

@implementation JackBaseViewController
@synthesize navTitle;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = kDefaultBackgroundColor;//kRGB(241, 246, 250);
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth-60*2, 40)];
    titleLabel.text = navTitle;
    titleLabel.font = kAppFont(36/2);
    titleLabel.textColor = kRGB(119, 119, 121);
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 22, 22)];
    [backButton setBackgroundImage:[UIImage imageNamed:@"01back"] forState:UIControlStateNormal];
    [backButton setBackgroundImage:[UIImage imageNamed:@"01back"] forState:UIControlStateHighlighted];
    [backButton addTarget:self action:@selector(navigationBack) forControlEvents:UIControlEventTouchUpInside];
    if (self.navigationController.viewControllers.count == 1)
    {
        self.navigationItem.leftBarButtonItem = nil;
    }
    else
    {
        [self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:backButton]];
    }
    self.navigationItem.hidesBackButton = YES;
    
    self.navigationController.delegate = self;
//    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
//        self.navigationController.interactivePopGestureRecognizer.delegate = self;
//    }

    self.navigationController.navigationBar.translucent = NO;
    
    //UIColor *topBackColor = kWhiteColor;
    //[self.navigationController.navigationBar setBackgroundImage:[JackTool imageWithColor:topBackColor] forBarMetrics:UIBarMetricsDefault];
    //self.navigationController.navigationBar.shadowImage = [JackTool imageWithColor:kRGBA(200, 199, 213, 0.5f)];//[UIImage new];
}



- (void)navigationBack
{
    if ([NSThread isMainThread]) {
        NSLog(@"Yay!");
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        NSLog(@"Humph, switching to main");
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
    }
    //[self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer == self.navigationController.interactivePopGestureRecognizer) {
        return [self.navigationController.viewControllers count] > 1;
    } else {
        return YES;
    }
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    if (self.navigationController.delegate != self) {
        self.navigationController.delegate = self;
    }
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    if (self.navigationController.delegate == self) {
        self.navigationController.delegate = nil;
    }
}

-(void) dealloc
{
    if (self.navigationController.delegate == self)
    {
        self.navigationController.delegate = nil;
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}


@end
